package inc.jmc.cyberboard.ui.print.ticket;

import inc.jmc.cyberboard.common.NumFormat;
import inc.jmc.cyberboard.dao.Dao;
import inc.jmc.cyberboard.dao.HistoricoVentasDao;
import inc.jmc.cyberboard.entitybeans.HistoricoVentas;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;

public class TicketGenerator {
	TicketTemplateVO template;
	String toString(DetalleStringVO vo){
		String espacios="                         ";
		if(vo.nombre.length()>14)
			vo.nombre = vo.nombre.substring(0,14);
		if(vo.cantidad.length()>4)
			vo.cantidad = vo.cantidad.substring(0,4);
		if(vo.subtotal.length()>9)
			vo.subtotal = vo.subtotal.substring(0,9);
		
		int c=4-vo.cantidad.length();
		int d=14-vo.nombre.length();
		int m=9-vo.subtotal.length();
		
		String rec=
		espacios.substring(0,c)+vo.cantidad+" "+
		vo.nombre+espacios.substring(0,d)+" "+
		espacios.substring(0,m)+vo.subtotal+"\n";
		return rec;
	}
	String toStringSubtotal(String dato,String valor){
		String espacios="                         ";
		int dat=dato.length()+valor.length();
		int d=29-dat;
		String rec=
		dato+espacios.substring(0,d)+valor+"\n";
		return rec;
	}
	
	public TicketGenerator(TicketTemplateVO template) {
		this.template = template;
	}
	String processDetalle(ArrayList detalleList){
		int recs=detalleList.size();
		StringBuffer sb= new StringBuffer();
		for (int i = 0; i < recs; i++) {
			ArrayList campos=(ArrayList)detalleList.get(i);
			DetalleStringVO vo=new DetalleStringVO();
			vo.nombre= (String)campos.get(0);
			vo.cantidad= ""+(Integer)campos.get(2);
			vo.subtotal= ""+NumFormat.money((Float)campos.get(3));
			sb.append(this.toString(vo));
		}

		return sb.toString();
	}
	public String generateTicket(HistoricoVentas ventaPojo, String nombre,
			String subtotal,String iva,String total) {
		Integer idVenta = ventaPojo.getIdVenta();
		Connection conn = null;
        ArrayList detalleList =new ArrayList();
        try{
            conn = Dao.getConnection();
            HistoricoVentasDao dao = new  HistoricoVentasDao();
            detalleList = dao.getDetalleVenta(conn,idVenta.intValue());
            conn.close();
            
        }
        catch (SQLException ex)
        {
            ex.printStackTrace();
        
        }
        catch (ClassNotFoundException ex)
        {
            ex.printStackTrace();
        
        }
        StringBuffer sb=new StringBuffer();
        sb.append(template.getNombre()+"\n");
        sb.append(template.getDomicilio()+"\n");
        String time = template.getTimeStamp();
        time = time.replaceAll("numero",""+ventaPojo.getIdVenta());
        time = time.replaceAll("empleado",""+nombre);
        time = time.replaceAll("fechacompra",""+NumFormat.sdf_ddMMyyyy_hhmmss.format(new Date()));
        sb.append(time+"\n");
        sb.append(template.getEncabezados()+"\n");
        sb.append(processDetalle(detalleList));
        sb.append("=============================\n");
        sb.append(toStringSubtotal("SUBTOTAL",subtotal)+"\n");
        sb.append(toStringSubtotal("IVA",iva)+"\n");
        sb.append(toStringSubtotal("TOTAL",total)+"\n");
        sb.append("=============================\n");
        sb.append(template.getPieDePagina());
        return sb.toString();
		
        
	
	}
	
	

}
